package com.mingmen.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.model.WeiHuModel;

import java.util.ArrayList;
import java.util.List;


public class NewWeiHuListAdapter extends RecyclerView.Adapter<NewWeiHuListAdapter.ViewHolder> {
    List<WeiHuModel.DataBean.CommoditiesBean> myLiveList = new ArrayList<>();
    Context mContext;
    private OnItemClickListener mOnItemClickListener;
    private int checkedPosition = -1;

    //    WeiHuModel.DataBean
    //recycleview标签
    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int pos, List<WeiHuModel.DataBean.CommoditiesBean> myLiveList);
    }


    public NewWeiHuListAdapter(Context context, List<WeiHuModel.DataBean.CommoditiesBean> myLiveList) {
        this.mContext = context;
        this.myLiveList = myLiveList;

    }

    public void setDataList(List<WeiHuModel.DataBean.CommoditiesBean> list) {
        myLiveList = list;

        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weihu, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        WeiHuModel.DataBean.CommoditiesBean dataBean = myLiveList.get(position);
        holder.commodityName.setText(myLiveList.get(position).getCommodityName());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.text_manager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.text_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        holder.text_kucun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, position, myLiveList);
            }
        });
        Glide.with(mContext).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);
        holder.text_spec.setText("规格: " + dataBean.getSpec());
        holder.description.setText(dataBean.getDescription());
        holder.inventory.setText(dataBean.getInventory());
        holder.price.setText(dataBean.getPrice());
        holder.text_price.setText(dataBean.getTodayPrice());
        holder.text_kucun.setText(dataBean.getAddKucun());
        Log.d("datastate", dataBean.getState());
        if (dataBean.getState().equals("2")) {
            holder.layout_states.setBackground(mContext.getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            holder.img_arrow.setImageResource(R.mipmap.arrowblue_bottom);
            holder.text_color.setText("已上架");
            holder.text_color.setTextColor(mContext.getResources().getColor(R.color.normal_bluetextcolor));
        } else {
            holder.layout_states.setBackground(mContext.getResources().getDrawable(R.drawable.shape_normal_red_white));
            holder.img_arrow.setImageResource(R.mipmap.arrow_red_sanjiaop);
            holder.text_color.setText("未上架");
            holder.text_color.setTextColor(Color.parseColor("#B4170F"));
            holder.text_price.setText("----");
            holder.text_kucun.setText("----");
            holder.text_price.setEnabled(false);
            holder.text_kucun.setEnabled(false);
        }

    }

    @Override
    public int getItemCount() {
        return myLiveList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView commodityName;
        LinearLayout content;
        TextView text_manager;

        ImageView ivAvatar;

        TextView text_spec;

        TextView description;

        TextView inventory;

        TextView price;
        LinearLayout layout_states;
        ImageView img_arrow;
        TextView text_color;
        TextView text_price;
        TextView text_kucun;

        public ViewHolder(View itemView) {
            super(itemView);

            commodityName = (TextView) itemView.findViewById(R.id.commodityName);
            content = itemView.findViewById(R.id.content);
            text_manager = itemView.findViewById(R.id.text_manager);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            text_spec = itemView.findViewById(R.id.text_spec);
            description = itemView.findViewById(R.id.description);
            inventory = itemView.findViewById(R.id.inventory);
            price = itemView.findViewById(R.id.price);
            layout_states = itemView.findViewById(R.id.layout_states);
            img_arrow = itemView.findViewById(R.id.img_arrow);
            text_color = itemView.findViewById(R.id.text_color);
            text_price = itemView.findViewById(R.id.text_price);
            text_kucun = itemView.findViewById(R.id.text_kucun);
        }
    }


}
